package com.nju.bean.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import android.util.Log;


import com.nju.bean.client.string.XmlHandler;
import com.nju.bean.client.string.XmlMaker;

public class RefreshManager extends NetManager{
	private List<HashMap<String,String>> topicList;
	private List<String> topicTittleList;
	private List<HashMap<String,Object>> newsList;
	private List<HashMap<String,Object>> replyList;
	
	public static final String REQUEST_TOPIC = "requestTopic";
	public static final String REQUEST_NEWS = "requestNews";
	public static final String OVERALL = "overall";
	public static final String LOCAL = "local";
	
	
	public RefreshManager(String address,int port){
		this.ipAdress = address;
		this.port = port;
	}
	
	public List<HashMap<String,String>> getTopicList(String type,double la,double lo) throws UnknownHostException, IOException{
		socket = new Socket(ipAdress,port);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"GB2312"));
		bw.write(XmlMaker.makeTopicRequest( type,la,lo));
		bw.flush();
		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"GB2312"));
		String line;
		String str = new String();
		for(line = br.readLine();line!=null;line = br.readLine()){
			str+=line;
		}
		br.close();
		bw.close();
		socket.close();
		XmlHandler xh = new XmlHandler(str);
		topicList = xh.getTopicList();
		topicTittleList = xh.getTopicTittleList();
		return topicList;
	}

	public List<String> getTopicTittleList() {
		return topicTittleList;
	}
	
	public List<HashMap<String,String>> getTopicList(){
		return topicList;
	}
	
	public List<HashMap<String,Object>> getNewsList(String topicId) throws UnknownHostException, IOException{
		socket = new Socket(ipAdress,port);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"GB2312"));
		bw.write(XmlMaker.makeSimpleText("requestNews", ""+topicId));
		bw.flush();
		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"GB2312"));
		String line;
		String str = new String();
		for(line = br.readLine();line!=null;line = br.readLine()){
			str+=line;
		}
		Log.v("rm", str);
		XmlHandler xh = new XmlHandler(str);
		newsList = xh.getNewsList();
		return newsList;
	}
	
	public List<HashMap<String,Object>> getNewsList(){
		return newsList;
	}
	public List<HashMap<String,Object>> getReply(String newsId) throws IOException{
		socket = new Socket(ipAdress,port);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"GB2312"));
		bw.write(XmlMaker.makeSimpleText("requestReply",newsId));
		bw.flush();
		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"GB2312"));
		String line;
		String str = new String();
		for(line = br.readLine();line!=null;line = br.readLine()){
			str+=line;
		}
		System.out.println(str);
		XmlHandler xh = new XmlHandler(str);
		replyList = xh.getReplyList();
		return replyList;
	}
}
